%Use to warp univariate tlrc masks to subject's native space

function warpmask(sub)

if sub < 10
    s = ['0',num2str(sub)]
else s = num2str(sub)
end

rois=14;

cd(['desisl_',s,'/desisl_',s,'.results'])


%%
%1. warp subject's aligned anatomical to TLRC space
unix(sprintf(['3dAllineate -prefix desisl_',s,'.anat_align_AllineateToTLRC '...
    '-1Dmatrix_save desisl_',s,'_AllineateToTLRC_warp_matrix.1D '...
    '-source desisl_',s,'.anat_align+orig. -base ../../TT_N27_SurfVol.nii']))

%%
%2. get the inverse of the warp matrix for warping from TLRC to subject
%space
unix(sprintf(['cat_matvec desisl_',s,'_AllineateToTLRC_warp_matrix.1D -I '...
    '>desisl_',s,'_AllineateToTLRC_invwarp_matrix.1D']))

%%
%3. apply inverse matrix to mask
unix(sprintf(['3dAllineate -1Dmatrix_apply desisl_',s,'_AllineateToTLRC_invwarp_matrix.1D '...
    '-prefix desisl_',s,'_hadvdid_voxp001_corrp05_mask '...
    '-source ../../groupdata/univariate/hadvdid_voxp.001_corrp.05_mask+tlrc. '...
    '-master BLOCK_81_hadvdid_blur6.stats.desisl_',s,'+orig']))

%%
%4. for each ROI apply inverse warp to go to subject space
for curroi=1:rois
    r=num2str(curroi)
unix(sprintf(['3dAllineate -1Dmatrix_apply desisl_',s,'_AllineateToTLRC_invwarp_matrix.1D '...
    '-prefix desisl_',s,'_hadvdid_voxp001_corrp05_roi',r,' '...
    '-source ../../groupdata/univariate/hadvdid_voxp.001_corrp.05_roi',r,'+tlrc. '...
    '-master BLOCK_81_hadvdid_blur6.stats.desisl_',s,'+orig']))
end


cd ../../

end

